package com.silence.whereeat.dao;

import com.silence.whereeat.model.StoreInfo;
import com.silence.whereeat.service.StoreInfoService;
import com.silence.whereeat.utils.DBUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 商店服务
 * Created by KIM on 2015/9/1.
 */
public class StoreInfoDao implements StoreInfoService {

    private DBUtils dbUtils;

    public StoreInfoDao() {
        dbUtils = new DBUtils();
    }


    public int addStore(StoreInfo storeInfo) {
        String sql = "INSERT INTO storeinfo (`store_name`,`store_address`,`store_phone`) VALUES (?,?,?)";
        List<Object> list = new ArrayList<Object>();
        list.add(storeInfo.getStore_name());
        list.add(storeInfo.getStore_address());
        list.add(storeInfo.getStore_phone());
        int result = -1;
        try {
            result = dbUtils.updateByPreParedStatement(sql, list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean isExist(int storeID) {
        String sql = "SELECT COUNT(*) FROM storeinfo WHERE store_id=?";
        List<Object> list = new ArrayList<Object>();
        list.add(storeID);
        int result = -1;
        try {
            result = Integer.parseInt(dbUtils.querySingleResult(sql, list).get("COUNT(*)").toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result == 1;
    }

    public int deleteStore(int storeID) {
        String sql = "DELETE FROM storeinfo WHERE store_id=?";
        List<Object> list = new ArrayList<Object>();
        list.add(storeID);
        int result = -1;
        try {
            result = dbUtils.updateByPreParedStatement(sql, list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public int editStore(StoreInfo storeInfo) {
        String sql = "UPDATE storeinfo " +
                "SET store_name=?," +
                "store_introduction=?," +
                "store_image=?," +
                "store_address=?," +
                "store_phone=?," +
                "store_business_hours=?" +
                "WHERE store_id=?;";
        List<Object> list = new ArrayList<Object>();
        list.add(storeInfo.getStore_name());
        list.add(storeInfo.getStore_introduction());
        list.add(storeInfo.getStore_image());
        list.add(storeInfo.getStore_address());
        list.add(storeInfo.getStore_phone());
        list.add(storeInfo.getStore_business_hours());
        list.add(storeInfo.getStore_id());
        int result = -1;
        try {
            result = dbUtils.updateByPreParedStatement(sql, list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public StoreInfo getStoreById(int storeID) {
        String sql = "SELECT * " +
                "FROM storeinfo " +
                "WHERE store_id = ?;";
        StoreInfo storeInfo = null;
        List<Object> list = new ArrayList<Object>();
        list.add(storeID);
        try {
            storeInfo = dbUtils.querySingleResult(sql, list, StoreInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return storeInfo;
    }

    public List<StoreInfo> getStoreByName(String storeName) {
        String sql = "SELECT * " +
                "FROM storeinfo " +
                "WHERE store_name LIKE ?;";
        String str = "%";
        for (int i = 0; i < storeName.length(); i++) {
            str += storeName.charAt(i) + "%";
        }
        List<Object> list = new ArrayList<Object>();
        list.add(str);
        List<StoreInfo> storeInfos = null;
        try {
            storeInfos = dbUtils.queryResultSet(sql, list, StoreInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return storeInfos;
    }

    public List<StoreInfo> getStores(int pageNumber) {
        String sql = "SELECT * " +
                "FROM storeinfo " +
                "LIMIT ?,?;";
        List<Object> list = new ArrayList<Object>();
        list.add(pageNumber * 10);
        list.add(pageNumber * 10 + 9);
        List<StoreInfo> storeInfos = null;
        try {
            storeInfos = dbUtils.queryResultSet(sql, list, StoreInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return storeInfos;
    }

    public List<StoreInfo> getStoresOrderByLike(int pageNumber) {
        String sql = "SELECT * " +
                "FROM storeinfo " +
                "ORDER BY store_like " +
                "LIMIT ?,?;";
        List<Object> list = new ArrayList<Object>();
        list.add(pageNumber * 10);
        list.add(pageNumber * 10 + 9);
        List<StoreInfo> storeInfos = null;
        try {
            storeInfos = dbUtils.queryResultSet(sql, list, StoreInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return storeInfos;
    }

    public void close() {
        dbUtils.close();
    }
}
